package com.wpre.lotterysystem.dao.mapper;

import com.wpre.lotterysystem.dao.dataobject.Encrypt;
import com.wpre.lotterysystem.dao.dataobject.UserDO;
import com.wpre.lotterysystem.service.dto.UserDTO;
import jakarta.validation.constraints.NotBlank;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-12-14
 * Time: 15:21
 */
@Mapper
public interface UserMapper {

    /**
     * 查询邮箱绑定人数
     *
     * @param email
     * @return
     */
    @Select("select COUNT(*) from user where email=#{email}")
    int countByMail(@Param("email") String email);

    @Select("select count(*) from user where phone_number = #{phoneNumber}")
    int countByPhone(@Param("phoneNumber") Encrypt phoneNumber);

    @Insert("insert into user (user_name,email,phone_number,password,identity) values (#{userName},#{email},#{phoneNumber},#{password},#{identity})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
        //自动生成的主键设置给java中的
    void insert(UserDO userDO);

    /**
     * 根据电话查询
     *
     * @param phoneNumber
     * @return
     */
    @Select("select * from user where phone_number = #{phoneNumber}")
    UserDO selectByPhoneNumber(@Param("phoneNumber") Encrypt phoneNumber);

    /**
     * 根据邮箱查询
     *
     * @param email
     * @return
     */
    @Select("select * from user where email = #{email}")
    UserDO selectByMail(@Param("email") String email);

    /**
     * 根据身份查询人员列表,如果身份信息为空,查询各个人员身份列表
     *
     * @param identity
     * @return
     */
    @Select("<script>" +
            " select * from user" +
            " <if test=\"identity!=null\">" +
            "    where identity = #{identity}" +
            " </if>" +
            " order by id desc" +
            " </script>")
    List<UserDO> selectUserListByIdentity(@Param("identity") String identity);

    /**
     * @param id 查看库中是否存在这些id
     * @return 库中存在的id
     */
    @Select("<script>" +
            " select id from user" +
            " where id in " +
            " <foreach item='item' collection='items' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " </script>")
    List<Long> selectExistByIds(@Param("items") List<Long> id);

    @Select("<script>" +
            " select * from user" +
            " where id in " +
            " <foreach item='item' collection='items' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " </script>")
    List<UserDO> batchSelectByIds(@Param("items") List<Long> ids);

}
